package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.*;
import com.sxkf.template.service.PostLikeService;
import com.sxkf.template.service.ProductLikeService;
import com.sxkf.template.service.ProductService;
import com.sxkf.template.vo.request.CollectDeleteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品收藏 前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-10 16:21:00
 */
@RestController
@RequestMapping("/productLike")
@Api(tags = "商品收藏")
public class ProductLikeController {
    @Resource
    private ProductLikeService productLikeService;
    @Resource
    private ProductService productService;

    @ApiOperation(value = "收藏")
    @PostMapping(value = "/add")
    public R<?> add(@RequestBody ProductLike productLike){
        productLikeService.add(productLike);
        return R.success();
    }


    @ApiOperation(value = "取消收藏")
    @GetMapping(value = "/del")
    public R<?> del(@RequestParam String id){
        productLikeService.removeById(id);
        return R.success();
    }


    @ApiOperation(value = "是否收藏")
    @GetMapping(value = "/isAdd")
    public R<?> isAdd(@RequestParam String userId,
                      @RequestParam String productId){
        Boolean res = productLikeService.isAdd(userId, productId);
        return R.success(res);
    }


    @ApiOperation(value = "分页查点赞列表")
    @GetMapping(value = "/page")
    public R<IPage<Product>> page(@RequestParam Integer pageNo,
                                  @RequestParam Integer pageSize,
                                  @RequestParam String userId){

        Page<ProductLike> page = productLikeService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<ProductLike>()
                .eq(ProductLike::getUserId, userId));

        Page<Product> productPage = new Page<>(pageNo, pageSize);

        if (page.getTotal() == 0){
            return R.success(productPage);
        }

        List<ProductLike> records = page.getRecords();
        List<String> productIdList = records.stream().map(ProductLike::getProductId).collect(Collectors.toList());

        List<Product> productList = productService.listByIds(productIdList);

        BeanUtils.copyProperties(page, productPage);
        productPage.setRecords(productList);

        return R.success(productPage);

    }

    @ApiOperation(value = "批量删除")
    @PostMapping(value = "/deleteBatch")
    public R<?> deleteBatch(@RequestBody CollectDeleteVO collectDeleteVO){
        productLikeService.remove(new LambdaQueryWrapper<ProductLike>()
                .eq(ProductLike::getUserId,collectDeleteVO.getUserId())
                .in(ProductLike::getProductId,collectDeleteVO.getTargetIds()));
        return R.success();
    }

}
